package mbp.message;

import java.util.Date;
import java.util.Random;

import mbp.core.BasicUUID;
import mbp.core.Intention;
import mbp.core.Resource;
import mbp.core.ResourceValidation;
import mbp.core.Service;

/**
 * Klasa przeznaczona do klonowania obiektow systemu MBP. Wykorzystywana przy tworzeniu argumentow
 * dla nowo tworzonych agentow. Celem jest aby nowy agent nie wspoldzielil pamieci z innym agentem.
 * @author Wojciech Wilanski
 *
 */
public class Copier
{
	/**
	 * Klonuje obiekt Resource
	 * @param rcToClone
	 * @return
	 */
	public static Resource cloneResource(Resource rcToClone)
	{
		ObjectBuilder objectBuilder = new ObjectBuilder();
		resource rci = objectBuilder.getResourceInfo(rcToClone);
		Resource rc = objectBuilder.getResource(rci);
		return rc;
	}
	
	/**
	 * Kopiuje obiekt Resource. Two
	 * @param rcToClone
	 * @return
	 */
	public static Resource copyResource(Resource rcToClone)
	{
		ObjectBuilder objectBuilder = new ObjectBuilder();
		resource rci = objectBuilder.getResourceInfo(rcToClone);
		
		Date dt = new Date();
		Random random = new Random(); 
		BasicUUID basicUUID = new BasicUUID(0x01010101^dt.getTime(),dt.getTime(),random.nextInt());
		rci.resourceid = basicUUID.toString();
		
		Resource rc = objectBuilder.getResource(rci);
		return rc;
	}
	
	/**
	 * Klonuje obiekt ResourceValidation
	 * @param rcvToClone
	 * @return
	 */
	public static ResourceValidation cloneResourceValidation(ResourceValidation rcvToClone)
	{
		ObjectBuilder objectBuilder = new ObjectBuilder();
		validation rcvi = objectBuilder.getValidationInfo(rcvToClone);
		ResourceValidation rcv = objectBuilder.getResourceValidation(rcvi);
		return rcv;
	}
	
	/**
	 * Klonuje obiekt Intention
	 * @param intToClone
	 * @return
	 */
	public static Intention cloneIntention(Intention intToClone)
	{
		ObjectBuilder objectBuilder = new ObjectBuilder();
		intention intni = objectBuilder.getIntentionInfo(intToClone);
		Intention intn = objectBuilder.getIntention(intni);
		return intn;
	}
	
	public static contract cloneContractInfo(contract cntrToClone)
	{
		contract cntri = new contract();
		cntri.agent = new String(cntrToClone.agent);
		cntri.type = new String(cntrToClone.type);
		cntri.client = new String(cntrToClone.client);
		cntri.server = new String(cntrToClone.server);
		cntri.signed = new String("");
		cntri.state = new String(cntrToClone.state);
		cntri.uuid = new String(cntrToClone.uuid);
		
		return cntri;
	}
	
	public static Service cloneService(Service srvToClone)
	{
		ObjectBuilder objectBuilder = new ObjectBuilder();
		service iservice = objectBuilder.getServiceInfo(srvToClone);
		Service service = objectBuilder.getService(iservice);
		return service;
	}

}
